package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbBrandService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("typeTemplate")
public class TypeTemplateController {
    Logger logger = LoggerFactory.getLogger(TypeTemplateController.class);
    @DubboReference
    private TbTypeTemplateService tbTypeTemplateService;
    @DubboReference
    private TbBrandService         tbBrandService;
    @DubboReference
    private TbSpecificationService tbSpecificationService;
    /*商品类型模板管理页面跳转*/
    @RequestMapping("runTypeTemplate")
    public String runSeller(Model model){
        /*查询tb_brand*/
        List<TbBrand> brandList = tbBrandService.selectAll();
        /*查询tb_specification*/
        List<TbSpecification> specificationList =tbSpecificationService.selectAll();
        model.addAttribute("brandList",brandList);
        model.addAttribute("specificationList",specificationList);
        return "admin/type_template";
    }
    /*商品类型模板管理页面展示*/
    @RequestMapping("selectInfoLimitServer")
    @ResponseBody
    public PageResult<TbTypeTemplate> selectInfoLimitServer(TbTypeTemplate tbTypeTemplate, @RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                                                             @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        PageResult<TbTypeTemplate> pageBean = tbTypeTemplateService.selectInfoLimitServer(tbTypeTemplate,pageNumber, pageSize);
        return pageBean;
    }
    /*添加模板*/
    @RequestMapping("addTypeTemplate")
    @ResponseBody
    public AjaxResult addTypeTemplate(TbTypeTemplate tbTypeTemplate){

        try {
            int i = tbTypeTemplateService.addTypeTemplate(tbTypeTemplate);
            if (i> 0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("添加失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }

    /*修改回显*/
    @RequestMapping("getUpdateTypeTemplate/{id}")
    @ResponseBody
    public AjaxResult getUpdateTypeTemplate(@PathVariable("id") Long id){
//        更具id查询tb_type_template
        try {
            TbTypeTemplate template = tbTypeTemplateService.getSelctById(id);
            if (template != null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(template);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("修改回显失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }

    /*执行修改*/
    @RequestMapping("updateTypeTemplate")
    @ResponseBody
    public AjaxResult updateTypeTemplate(TbTypeTemplate tbTypeTemplate){
//        更具id查询tb_type_template
        try {
            int i = tbTypeTemplateService.updateTypeTemplate(tbTypeTemplate);
            if (i > 0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("修改回显失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }

    /*删除*/
    @RequestMapping("deleteTypeTemplate/{id}")
    @ResponseBody
    public AjaxResult deleteTypeTemplate(@PathVariable("id") Long id){
        try {
            int i = tbTypeTemplateService.deleteTypeTemplate(id);
            if (i> 0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("删除失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
}
